// 利用缓存做假数据
const utils = require('@/utils')
const mapTools = require('@/utils/mapTools')
const dataMap = require('@/utils/dataMap')

const v2Label = mapTools.key2Value(dataMap.DATA_MAP)

module.exports = {
  data() {
    return {

    }
  },
  methods: {
    filterPassData(result) {
      return result.filter((item) => item.status === 2)
    },
    getFakeDataList(key) {
      const result = utils.getStorage(key, 0)
      if (!result) return
      const v = this.filterPassData(result)
      mapTools.dealResult2Map(v, v2Label)
      return v
    },
    getFakeDataDetail(key, id) {
      const result = utils.getStorage(key, 0)
      if (!result) return
      const target = result.find((item) => item.id === id)
      const v = this.filterPassData([target])
      mapTools.dealResult2Map(v, v2Label)
      return v
    },
    issueFakeData(key, value) {
      const currentStorage = utils.getStorage(key, 0)
      let result = []
      value.status = 0 // 信息状态
      value.createTime = '2021-11-19'
      value.views = 0
      value.types = ['抗静电']
      if (!currentStorage) {
        value.id = 0
        result = [value]
      } else {
        value.id = currentStorage.length
        result = result.concat(currentStorage)
        result.push(value)
      }
      utils.setStorage(key, result, 0)
    },
    deleteFake(key, id) {
      const currentStorage = utils.getStorage(key, 0)
      const updatedResult = currentStorage.filter((item) => item.id !== id)
      utils.setStorage(key, updatedResult, 0)
    },
    updateFake(key, id, result) {
      const currentStorage = utils.getStorage(key, 0)
      const idx = currentStorage.findIndex((item) => item.id === id)
      currentStorage.splice(idx, 1, result)
      utils.setStorage(key, currentStorage, 0)
    },
    getFakeData(key) {
      return utils.getStorage(key, 0)
    }
  }
}